package com.banciyuan.bcywebview.biz.detail.post;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.base.view.recycleview.OnResultScrollListener;
import com.banciyuan.bcywebview.biz.detail.complex.ComplexDetailActivity;
import com.banciyuan.bcywebview.biz.groupdetail.NewTeamDetailActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;

import de.greenrobot.daoexample.model.Profile;

/**
 * Created by squall on 2016/2/18.
 */
public class PostDetailActivity extends ComplexDetailActivity {

    private LinearLayout info_line;
    private TextView title;
    private TextView post_nums;
    private TextView post_title;
    private ImageView ivActionbarRightAction;
    private int scroll_Y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initActionbar() {
        findViewById(R.id.action_title).setVisibility(View.VISIBLE);
        findViewById(R.id.base_action_bar_home).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseObserver.getInstance().notifyWatchers(BaseObserver.FINISH_EVENT);
            }
        });
        ivActionbarRightAction = (ImageView) findViewById(R.id.iv_actionbar_right_action);
        info_line = (LinearLayout) findViewById(R.id.info_line);
        title = (TextView) findViewById(R.id.title);
        post_nums = (TextView) findViewById(R.id.post_num);
        post_title = (TextView) findViewById(R.id.post_title);
    }

    @Override
    protected void initUi() {
        super.initUi();
        rl_top.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void renderDetailUi() {
        super.renderDetailUi();
        processRightAction();
        if (complex.getGroup() != null && !TextUtils.isEmpty(complex.getGroup().getName())) {
            post_title.setText(Html.fromHtml(complex.getGroup().getName()));
        }
        if (complex.getGroup() != null && !TextUtils.isEmpty(complex.getGroup().getPost_num())) {
            post_nums.setText(String.format(getString(R.string.answer_unit_without_n), complex.getGroup().getPost_num()));
        }
    }

    private void processRightAction() {
        Profile profile = complex.getProfile();
        if (null != profile) {
            ivActionbarRightAction.setVisibility(View.VISIBLE);
            if (StringUtil.notNullEqual(profile.getUid(), UserDataHelper.getInstance(this).getUid())) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ivActionbarRightAction.setImageDrawable(getResources().getDrawable(R.drawable.more_black, getTheme()));
                } else {
                    ivActionbarRightAction.setImageDrawable(getResources().getDrawable(R.drawable.more_black));
                }
            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ivActionbarRightAction.setImageDrawable(getResources().getDrawable(R.drawable.share_black, getTheme()));
                } else {
                    ivActionbarRightAction.setImageDrawable(getResources().getDrawable(R.drawable.share_black));
                }
            }
            ivActionbarRightAction.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showShare();
                }
            });
        } else {
            ivActionbarRightAction.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    protected void initAction() {
        info_line.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(PostDetailActivity.this, NewTeamDetailActivity.class, complex.getGid());
            }
        });

        recyclerView.addOnScrollListener(new OnResultScrollListener() {
            @Override
            public void onBottom() {
                if (!is_end) {
                    page++;
                    getComment();
                }
            }

            @Override
            public void onTop() {
                info_line.setVisibility(View.GONE);
                info_line.setAlpha(0f);
                title.setAlpha(1f);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                scroll_Y += dy;
                float alpha1 = 1f - (float) (scroll_Y * 0.01);
                float alpha2 = -1f + (float) (scroll_Y * 0.01);
                if (alpha1 >= 0) {
                    if (title.getVisibility() == View.GONE) {
                        title.setVisibility(View.VISIBLE);
                    }
                    info_line.setVisibility(View.GONE);
                    title.setAlpha(alpha1);
                } else if (alpha1 >= 1) {
                    title.setAlpha(1f);
                }
                if (alpha2 >= 0) {
                    if (info_line.getVisibility() == View.GONE) {
                        info_line.setVisibility(View.VISIBLE);
                    }
                    title.setVisibility(View.GONE);
                    info_line.setAlpha(alpha2);
                } else if (alpha2 >= 1) {
                    info_line.setAlpha(1f);
                }
            }
        });

    }
}